/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.andkulikov.transitionseverywhere.slice;

import com.andkulikov.transitionseverywhere.ResourceTable;
import com.transitionseverywhere.Transition;
import com.transitionseverywhere.TransitionManager;
import com.transitionseverywhere.TransitionValues;
import harmony.util.IntProperty;
import com.transitionseverywhere.utils.ProgressAnimator;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.utils.LightweightMap;
import ohos.utils.ObjectAttribute;

import java.util.ArrayList;

public class CustomTransitionSlice extends AbilitySlice implements Component.ClickedListener {
    private DependentLayout mDlViewRoot;
    private ProgressBar mProgress;
    private ProgressTransition mProgressTransition = new ProgressTransition();

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer layout = (ComponentContainer) LayoutScatter.getInstance(this).
                parse(ResourceTable.Layout_slice_custom_transition, null, false);
        mDlViewRoot = (DependentLayout) layout.findComponentById(com.andkulikov.transitionseverywhere.ResourceTable.Id_dlViewRoot);
        Object tagMap = new LightweightMap<String, ArrayList<Transition>>();
        mDlViewRoot.setTag(tagMap);
        mProgress = (ProgressBar) layout.findComponentById(ResourceTable.Id_progress);
        mProgress.setProgressColor(Color.RED);
        Button btnPlus = (Button) layout.findComponentById(ResourceTable.Id_btnPlus);
        Button btnMinus = (Button) layout.findComponentById(ResourceTable.Id_btnMinus);
        ShapeElement background_color_element = new ShapeElement();
        background_color_element.setShape(ShapeElement.RECTANGLE);
        background_color_element.setRgbColor(new RgbColor(255, 0, 0));
        background_color_element.setBounds(0, 0, btnPlus.getRight(), btnPlus.getBottom());
        btnPlus.setBackground(background_color_element);
        btnMinus.setBackground(background_color_element);
        btnPlus.setClickedListener(this::onClick);
        btnMinus.setClickedListener(this::onClick);
        super.setUIContent(layout);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_btnPlus:
                ProgressTransition.isAlternate = true;
                TransitionManager.beginDelayedTransition(mDlViewRoot, mProgressTransition);
                break;
            case ResourceTable.Id_btnMinus:
                ProgressTransition.isAlternate = false;
                TransitionManager.beginDelayedTransition(mDlViewRoot, mProgressTransition);
                break;
        }
    }

    private static class ProgressTransition extends Transition {
        private static final ObjectAttribute<ProgressBar, Integer> PROGRESS_PROPERTY = new IntProperty<ProgressBar>() {

            @Override
            public void setValue(ProgressBar progressBar, int value) {
                progressBar.setProgressValue(value);
            }

            @Override
            public Integer get(ProgressBar progressBar) {
                return progressBar.getProgress();
            }
        };
        private static final String PROPNAME_PROGRESS = "ProgressTransition:progress";

        @Override
        public void captureStartValues(TransitionValues transitionValues) {
            captureValues(transitionValues);
        }

        static boolean isAlternate;

        @Override
        public void captureEndValues(TransitionValues transitionValues) {
            if (transitionValues.view instanceof ProgressBar) {
                // save current progress in the values map
                ProgressBar progressBar = ((ProgressBar) transitionValues.view);
                transitionValues.values.put(PROPNAME_PROGRESS, isAlternate ? Math.max(0, Math.min(100, progressBar.getProgress() + 30))
                        : Math.max(0, Math.min(100, progressBar.getProgress() - 30)));
            }
        }

        private void captureValues(TransitionValues transitionValues) {
            if (transitionValues.view instanceof ProgressBar) {
                // save current progress in the values map
                ProgressBar progressBar = ((ProgressBar) transitionValues.view);
                transitionValues.values.put(PROPNAME_PROGRESS, progressBar.getProgress());
            }
        }

        @Override
        public Animator createAnimator(ComponentContainer sceneRoot, TransitionValues startValues, TransitionValues endValues) {
            Animator animator = null;
            if (startValues != null && endValues != null && endValues.view instanceof ProgressBar) {
                ProgressBar progressBar = (ProgressBar) endValues.view;
                int start = (Integer) startValues.values.get(PROPNAME_PROGRESS);
                int end = (Integer) endValues.values.get(PROPNAME_PROGRESS);
                if (start != end) {
                    // first of all we need to return the start value, because right now
                    // the view is have the end value
                    progressBar.setProgressValue(start);
                    // create animator with our progressBar, property and end value
                    animator = ProgressAnimator.ofProgress(progressBar, PROGRESS_PROPERTY, start, end);
                    return animator;
                }
            }
            return animator;
        }
    }
}
